<?php
namespace Home\Controller;
use Think\Controller;
class UserAddressController extends Controller 
{
    public function add()
    {
    	if(IS_POST)
    	{
    		$model = D('Admin/UserAddress');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    			    if(I('post.ajax')){
    			        $model = D('Admin/UserAddress');
                        $newAddress = $model->relation(true)->where('id='.$id)->find();
                        exit(json_encode($newAddress));
                    }else{
                        $this->success('添加成功！', U('lst?p='.I('get.p')));
                        exit;
                    }

    			}
    		}
    		$this->error($model->getError());
    	}

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '添加',
			'_page_btn_name' => '收货地址列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display('My/address');
    }
    public function edit()
    {
    	if(IS_POST)
    	{
    		$model = D('Admin/UserAddress');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->where(array('id'=>array('eq',I('post.id'))))->save()!== FALSE)
    			{
                    if(IS_AJAX){
                        $model = D('Admin/UserAddress');
                        $newAddress = $model->relation(true)->where('id='.I('post.id'))->find();
                        exit(json_encode($newAddress));
                    }else{
                        $this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
                        exit;
                    }

    			}
    		}
    		$this->error($model->getError());
    	}
        $model = D('Admin/UserAddress');
        $data = $model->search();
        $info = $model->find(I('get.id'));
        if(IS_AJAX){
            echo json_encode($info);
        } else {
            $this->assign('info',$info);
            $this->assign('data', $data['data']);
            // 设置页面中的信息
            $this->assign(array(
                '_page_title' => '修改',
                '_page_btn_name' => '修改收货地址',
                '_page_btn_link' => U('lst'),
            ));
            $this->display('My/address');
        }
    }
    //设置为默认地址
    public function setDefault(){
        $id = I('get.id',0);
        $m_id =session('m_id');
        $model = D('Admin/UserAddress');
        $model->where('member_id='.$m_id)->save(array('is_default'=>0));
        $model->where(array(
            'id'=>array('eq',$id),
            'member_id'=>array('eq',$m_id)
        ))->save(array('is_default'=>1));
        $this->success('设置成功',U('lst'));
    }
    public function delete()
    {
    	$model = D('Admin/UserAddress');
        $info  = $model -> where('id='.I('get.id'))->find();
        if($info['is_default']==1){
            $this->error('默认地址不能删除');
            exit;
        }
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function lst()
    {
    	$model = D('Admin/UserAddress');
    	$data = $model->search();
        if(IS_AJAX){
            $model = D('Admin/UserAddress');
            $info = $model->relation(true)
                ->where('id='.I('get.id'))->find();
           exit(json_encode($info));
        }
    	$this->assign(array(
    		'data' => $data['data'],
    		'page' => $data['page'],
    	));
		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '收货地址',
			'_page_btn_name' => '添加收货地址',
			'_page_btn_link' => U('add'),
		));
    	$this->display('My/address');
    }
    public function region(){
        $regionId = I('get.id',1);
        $regionModel = M('Region');
        $regionData = $regionModel->where(array(
            'parent_id'=>array('eq',$regionId),
        ))->select();
        echo json_encode($regionData);
    }

}